import { useEffect } from "react";
import useGetUserInfo from "./useGetUserInfo";
import { useLocation, useNavigate } from "react-router-dom";
import { isRegisterOrLogin, MANAGE_INDEX_PATHNAME,isNoNeed, LOGIN_PATHNAME } from "../router";
const useNavPage = (waitingLoadUser: boolean) => {
  const { pathname } = useLocation();
  const nav = useNavigate();
  //bug,退出之后,自动获取info,登录
    const { username } = useGetUserInfo();
    useEffect(() => {  
        //
        if (waitingLoadUser) return;        
        //已登录
        if (username) {            
            if (isRegisterOrLogin(pathname)) {
                nav(MANAGE_INDEX_PATHNAME);
              }
              return;
        }        
        //未登录
        if (isNoNeed(pathname)) {
            return
        } else {
            nav(LOGIN_PATHNAME)
        }
  }, [waitingLoadUser,username,pathname]);
};
export default useNavPage;
